#!/bin/bash
#################################################################
# @Description: 用例名称：虚拟化测试用例清理环境
# @Date : 2023-11-24
# @Last Modified by :
# @Last Modified time: 2023-11-27
#################################################################
CRTDIR=$(pwd)
res=0
for i in {1..10}; do
    vm_name="vm$i"
    # 检查虚拟机是否存在
    if virsh list --all | grep -q "$vm_name"; then
        # 销毁虚拟机，禁止回显
        virsh destroy "$vm_name" >/dev/null 2>&1
        sleep 5
        # 移除虚拟机定义，禁止回显
        virsh undefine "$vm_name" --nvram >/dev/null 2>&1
        sleep 5
    fi
done

rm -f /opt/vm{1..10}.xml || res=1
rm -f /opt/vm[0-9].qcow2 || res=1
rm -f passwd.txt || res=1
yum remove -y libvirt qemu bridge-utils python3-libvirt edk2-aarch64 >/dev/null || res=1
[ "$res" = "0" ] && echo -e "\033[32m-----------------虚拟化测试环境清理成功----------------\033[0m" || echo -e "\033[31m-----------------虚拟化测试环境清理失败，请手动处理！----------------\033[0m"
